//
//  SampuxRecordViewCell.m
//  Sampux
//
//  Created by rzm on 16/7/12.
//  Copyright © 2016年 rzm. All rights reserved.
//

#import "SampuxRecordViewCell.h"
#import "SPTimeValue.h"
#import "SPSetProgramInfo.h"

@interface SampuxRecordViewCell ()

@end

@implementation SampuxRecordViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupSubView];
    }
    return self;
}

- (void)setupSubView
{
    CGFloat font = 8.;
    if (IS_IPHONE_4_OR_LESS) {
        font = 12.;
    }else if (IS_IPHONE_5_OR_LESS)
    {
        font = 14.;
    }else if (IS_IPHONE_6)
    {
        font = 16.;
    }else
    {
        font = 16.;
    }
    _timeLab = [[UILabel alloc]initWithFrame:CGRectMake(Sampux6WScale(35) + Sampux6WScale(20), Sampux6HScale(5), kScreenWidth - Sampux6WScale(35) * 2 -  Sampux6WScale(20) , Sampux6HScale(30))];
    _timeLab.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_timeLab];
    _timeLab.backgroundColor = THSystemClearColor;
    _timeLab.textColor = THWhiteAlplaColor(0.8);
    _timeLab.font = THFont(font);
    _timeLab.text = @"2016-8-9";

    _nubLab = [UILabel createLabel];
    _nubLab.frame = CGRectMake(Sampux6HScale(20), Sampux6HScale(10), Sampux6HScale(20), Sampux6HScale(20));
    [self addSubview:_nubLab];
//    _nubLab.layer.borderWidth = 1;
//    _nubLab.layer.borderColor = THWhiteAlplaColor(0.6).CGColor;
    _nubLab.textAlignment = NSTextAlignmentCenter;
    _nubLab.textColor = THWhiteAlplaColor(0.8);
    _nubLab.font = THFont(font);
    
 
//    _imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, backView.height * 0.2, 20, 20)];
//    [backView addSubview:_imgView];
//        _imgView.backgroundColor = RandomColor;
    _nameLab = [UILabel createLabel];
    _nameLab.frame = CGRectMake(Sampux6WScale(35), Sampux6HScale(70) * 0.2 + _nubLab.bottom, Sampux6WScale(70), Sampux6HScale(70) * 0.6);
    [self addSubview:_nameLab];
    _nameLab.font = THFont(font);
    _nameLab.textColor = THWhiteAlplaColor(0.8);
    
    _isSuccessLab = [UILabel createLabel];
    _isSuccessLab.frame = CGRectMake(kScreenWidth - Sampux6WScale(40) - Sampux6WScale(35), Sampux6HScale(70) * 0.2 + _nubLab.bottom,  Sampux6WScale(40),Sampux6HScale(70) * 0.6);
    _isSuccessLab.textAlignment = NSTextAlignmentRight;
    [self addSubview:_isSuccessLab];
    _isSuccessLab.font = THFont(font);
    _isSuccessLab.textColor = THWhiteAlplaColor(0.8);
    
    _timebackView = [[UIView alloc]initWithFrame:CGRectMake(_nameLab.right, _nubLab.bottom + Sampux6HScale(5),_isSuccessLab.left - _nameLab.right, Sampux6HScale(60))];
    [self addSubview:_timebackView];
    _timebackView.backgroundColor = THSystemClearColor;
    
    _backView = [[UIView alloc]initWithFrame:CGRectMake(_nameLab.right, _nubLab.bottom,_isSuccessLab.left - _nameLab.right, Sampux6HScale(65))];
    [self addSubview:_backView];
    _backView.backgroundColor = THSystemClearColor;

    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(Sampux6WScale(35), _backView.bottom, kScreenWidth - Sampux6WScale(35)*2, 1)];
    [self addSubview:lineView];
    lineView.backgroundColor = THWhiteAlplaColor(0.2);
    [self createTimeView];
    [self createHeatModelView];
}

- (void)setTitleStr:(NSString *)titleStr
{
    _titleStr = titleStr;
    _nameLab.text = _titleStr;
}

- (void)createTimeView
{
    CGFloat font = 8.;
    if (IS_IPHONE_4_OR_LESS) {
        font = 12.;
    }else if (IS_IPHONE_5_OR_LESS)
    {
        font = 12.;
    }else if (IS_IPHONE_6)
    {
        font = 14.;
    }else
    {
        font = 16.;
    }
    _mornLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, Sampux6WScale(90), Sampux6HScale(20))];
    _mornLab.textAlignment = NSTextAlignmentCenter;

    _afterLab = [[UILabel alloc]initWithFrame:CGRectMake(0, _mornLab.bottom, Sampux6WScale(90),  Sampux6HScale(20))];
    _afterLab.textAlignment = NSTextAlignmentCenter;

    _noonLab = [[UILabel alloc]initWithFrame:CGRectMake(_timebackView.width - Sampux6WScale(90), 0, Sampux6WScale(90),  Sampux6HScale(20))];
    _noonLab.textAlignment = NSTextAlignmentCenter;
    _nightLab = [[UILabel alloc]initWithFrame:CGRectMake(_timebackView.width - Sampux6WScale(90), _noonLab.bottom, Sampux6WScale(90),  Sampux6HScale(20))];
    _nightLab.textAlignment = NSTextAlignmentCenter;

    _weekLab = [[UILabel alloc]initWithFrame:CGRectMake(- 20 , _nightLab.bottom, _timebackView.width +20,  Sampux6HScale(20))];
    _weekLab.textAlignment = NSTextAlignmentCenter;
    _mornLab.text = @"09:20  20℃";
    _noonLab.text = @"12:20  24℃";
    _afterLab.text = @"16:20  20℃";
    _nightLab.text = @"20:20  20℃";
    _weekLab.text = @"周一";

    _mornLab.textColor = THWhiteAlplaColor(0.6);
    _noonLab.textColor = THWhiteAlplaColor(0.6);
    _afterLab.textColor = THWhiteAlplaColor(0.6);
    _nightLab.textColor = THWhiteAlplaColor(0.6);
    _weekLab.textColor = THWhiteAlplaColor(0.6);

    _mornLab.font = THFont(font - 2);
    _noonLab.font = THFont(font - 2);
    _afterLab.font = THFont(font - 2);
    _nightLab.font = THFont(font - 2);
    _weekLab.font = THFont(font - 2);
    
    [_timebackView addSubview:_mornLab];
    [_timebackView addSubview:_afterLab];
    [_timebackView addSubview:_noonLab];
    [_timebackView addSubview:_nightLab];
    [_timebackView addSubview:_weekLab];
}

- (void)createTempView
{
    CGFloat font = 8.;
    if (IS_IPHONE_4_OR_LESS) {
        font = 12.;
    }else if (IS_IPHONE_5_OR_LESS)
    {
        font = 12.;
    }else if (IS_IPHONE_6)
    {
        font = 14.;
    }else
    {
        font = 16.;
    }
    _tempLab = [UILabel createLabel];
    _tempLab.frame = CGRectMake(0, _backView.height/2 - Sampux6HScale(20)/2, _backView.width, Sampux6HScale(20));
    [_backView addSubview:_tempLab];
    _tempLab.textAlignment = NSTextAlignmentCenter;
    _tempLab.text = @"20℃";
    _tempLab.font = THFont(font - 2);
    _tempLab.textColor = THWhiteAlplaColor(0.6);
}

- (void)createHeatModelView
{
    CGFloat font = 8.;
    if (IS_IPHONE_4_OR_LESS) {
        font = 12.;
    }else if (IS_IPHONE_5_OR_LESS)
    {
        font = 12.;
    }else if (IS_IPHONE_6)
    {
        font = 14.;
    }else
    {
        font = 16.;
    }
    _modelLab = [UILabel createLabel];
    _modelLab.frame = CGRectMake(0, _backView.height/2 - Sampux6HScale(20)/2, _backView.width, Sampux6HScale(20));
    [_backView addSubview:_modelLab];
    _modelLab.textAlignment = NSTextAlignmentCenter;
    _modelLab.text = @"手动";
    _modelLab.font = THFont(font);
    _modelLab.textColor = THWhiteAlplaColor(0.8);
}

- (void)setModel:(SPUserOperationModel *)model
{
    _model = model;
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate*confromTimesp = [NSDate dateWithTimeIntervalSince1970:_model.requestTime];
    NSString*confromTimespStr = [formatter stringFromDate:confromTimesp];
    _timeLab.text = confromTimespStr;
    if (_model.result == 1) {
        _isSuccessLab.text = @"成功";
    }else
    {
        _isSuccessLab.text = @"失败";
    }
    
    if (_model.type == 13) {//加热
        _backView.hidden = NO;
        _timebackView.hidden = YES;

        if (_model.heatMode == 1) {
            _nameLab.text = @"加热模式";
            _modelLab.text = @"速热";
            
        }else if (_model.heatMode == 2)
        {
            _nameLab.text = @"加热模式";
            _modelLab.text = @"舒适";
        }else
        {
            _nameLab.text = @"加热模式";
            _modelLab.text = @"节能";
        }
        
    }else if (_model.type == 3)//时段设置
    {
        _backView.hidden = YES;
        _timebackView.hidden = NO;
        _nameLab.text = @"时段设置";
        _mornLab.text = [self setTimeStyle:_model.programOnePoint andCValue:_model.programOneValue];
        _noonLab.text = [self setTimeStyle:_model.programTwoPoint andCValue:_model.programTwoValue];
        _afterLab.text = [self setTimeStyle:_model.programThreePoint andCValue:_model.programThreeValue];
        _nightLab.text = [self setTimeStyle:_model.programFourPoint andCValue:_model.programFourValue];
//        NSArray *weekArr = @[@"周日",@"周六",@"周五",@"周四",@"周三",@"周二",@"周一"];
        NSArray *weekArr = @[@"周一",@"周二",@"周三",@"周四",@"周五",@"周六",@"周日"];

        NSString *binnary = [SPSetProgramInfo toBinarySystemWithDecimalSystem:STRING_INTERGER(_model.programDay)];
        NSString *temp = nil;
        NSMutableArray *locationArr = [NSMutableArray array];
        for (int i = 0; i < binnary.length; i ++) {
            temp = [binnary substringWithRange:NSMakeRange(i, 1)];
            if ([temp isEqualToString:@"1"]) {
                [locationArr addObject:STRING_INT(i)];
            }
        }
        NSLog(@"locationArrlocationArr=== %@",locationArr);
        
        NSMutableArray *weekAll = [NSMutableArray array];
        if (locationArr.count>0) {
            for (int i = 0; i <locationArr.count; i ++ ) {
                NSString *obj = locationArr[i];
                NSInteger index = [obj intValue];
                if (index == 7) {
                    [weekAll removeAllObjects];
                }else
                {
                    [weekAll addObject:weekArr[index]];
                }
            }
            NSLog(@"weekAll === %@",weekAll);
            _weekLab.text = [weekAll componentsJoinedByString:@" "];
        }
    }else if (_model.type == 4)//开关
    {
        _backView.hidden = NO;
        _timebackView.hidden = YES;
        _nameLab.text = @"开关";

        if (_model.powerSwitch == 1) {
            _modelLab.text = @"开机";
        }else
        {
            _modelLab.text = @"关机";
        }

    }else if (_model.type == 1)//工作模式
    {
        _backView.hidden = NO;
        _timebackView.hidden = YES;
        _nameLab.text = @"工作模式";
        if (_model.heatMode == 3) {
            _modelLab.text = @"定时";
        }else
        {
            _modelLab.text = @"手动";
        }
    }else if (_model.type == 2)
    {
        _backView.hidden = NO;
        _timebackView.hidden = YES;
        _nameLab.text = @"温度设置";
        _modelLab.text = [NSString stringWithFormat:@"%ld℃",(long)_model.currentSetTempe];
    }else
    {
    }
}


- (void)layoutSubviews
{
    CGFloat font = 8.;
    if (IS_IPHONE_4_OR_LESS) {
        font = 12.;
    }else if (IS_IPHONE_5_OR_LESS)
    {
        font = 14.;
    }else if (IS_IPHONE_6)
    {
        font = 16.;
    }else
    {
        font = 16;
    }
    CGSize maxSize = CGSizeMake(MAXFLOAT, Sampux6HScale(20));
    CGSize strSize = [SampuxGlobalTool getSizeWithString:_nubLab.text andFont:THFont(font) andMaxSize:maxSize];
    _nubLab.frame = CGRectMake(Sampux6HScale(20), Sampux6HScale(10), strSize.width, Sampux6HScale(20));
}

- (NSString *)setTimeStyle:(NSInteger)time andCValue:(NSInteger)temp
{
   NSString *timeStr = [SPTimeValue setTimeValue:STRING_INTERGER(time)];
    return [NSString stringWithFormat:@"%@-%d℃",timeStr,temp];
}
@end
